// @ts-nocheck
import doRequestAction from "@/utils/request";
import * as API from "@/constants/api";
import Taro from "@tarojs/taro";

/**
 * 虚拟登录
 * @param param0
 * @returns
 */
export const vituralLogin = async () => {
  return doRequestAction({
    url: API.vituralLogin,
    data: {},
  });
};

/**
 *  手机号登录
 * @param param
 * @returns
 */
export const login = async ({ mobile, key }) => {
  return doRequestAction({
    url: API.login,
    data: { mobile, key },
    method: "POST",
    title: "",
  });
};

export const customerLogin = async ({ mobile, key }) => {
  return doRequestAction({
    url: API.customerLogin,
    data: { mobile, key },
    title: "",
  });
};

/**
 * 微信登录
 */
export const wxLogin = async () => {
  let wxLoginResult = "";
  await Taro.login({
    success: function (res) {
      if (res.code) {
        console.log("wxLogin,", res);
        wxLoginResult = res.code;
      } else {
        console.log("登录失败！" + res.errMsg);
      }
    },
  });
  return wxLoginResult;
};

export const queryOpenId = async () => {
  // let loginResult = {};
  const code = await wxLogin();
  console.log("code,", code);
  if (code == "") {
    console.log("登录失败！");
    return;
  }
  return doRequestAction({
    url: API.getOpenId,
    data: { code },
  });
};
//h5  登录迁移
export const loginWithCode = async ({ username, password, companyCode }) => {
  return doRequestAction({
    url: API.loginWithCode,
    data: { username, password, companyCode, type: "mobile" },
    title: "",
  });
};
